package com.baiyang.server.control.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.model.base.Notice;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.service.base.NoticeService;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/noticeOperation")
@Jurisdiction(value = "/noticeOperation")
public class NoticeCon {
	@Autowired
	private NoticeService noticeService = null;

	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "getPage")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Notice> getPage(@RequestBody JSONObject object){
		Letter<Notice> letter = new Letter<Notice>();
		try {
			PageEntity<Notice> result = noticeService.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根据公告ID获取html文件并转化为字符串
	 * @param notice
	 * @return
	 */
	@Jurisdiction(value = "getNotice")
	@RequestMapping(value = "/getNotice",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> getNotice(@RequestBody Notice notice){
		Letter<String> letter = new Letter<String>();
		try {
			String noticeId = notice.getId();
			String html = noticeService.getNotice(noticeId);
			if(DataTool.isBlank(html)){
				letter.setResult("FAIL");
				letter.setDetail("数据获取失败");
			}else{
				letter.setData(html);
				letter.setDetail("数据获取成功");
				letter.setResult("SUCCESS");
			}
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

     /**
	 * 根绝条件获取数据
	 * @return
	 */
    @Jurisdiction(value = "selectList")
	@RequestMapping(value = "/selectList",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Notice> selectList(@RequestBody Notice notice){
		Letter<Notice> letter = new Letter<Notice>();
		try {

			List<Notice> result = noticeService.selectList(notice);
			letter.setList(result);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根据条件获取一个
	 * @param notice
	 * @return
	 */
	@Jurisdiction(value = "selectOne")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Notice> selectOne(@RequestBody Notice notice){
		Letter<Notice> letter = new Letter<Notice>();
		try {
			Notice obj = noticeService.selectOne(notice);
			letter.setData(obj);
			letter.setDetail("数据获取成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 插入数据
	 * @param notice
	 * @return
	 */
	@Jurisdiction(value = "insert")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody Notice notice){
		Letter<String> letter = new Letter<String>();
		notice.setId(DataTool.createUUID());
		Map<String , String> sub = SubjectNotNull.subject(notice);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！" + sub.toString());
			return letter;
		}
		if(DataTool.isBlank(notice.getContent())){
			letter.setResult("FAIL");
			letter.setDetail("公告不能为空");
			return letter;
		}
		try {
			noticeService.insertNotice(notice,letter);

		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 批量插入数据
	 * @param notices
	 * @return
	 */
	@Jurisdiction(value = "insertBatch")
	@RequestMapping(value = "/insertBatch",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody List<Notice> notices){
		Letter<String> letter = new Letter<String>();
		try {
			noticeService.insertBatch(notices,letter);
			letter.setResult("SUCCESS");
			letter.setDetail("数据插入成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件进行删除
	 * @param notice
	 * @return
	 */
	@Jurisdiction(value = "delete")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody Notice notice){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(notice)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			noticeService.delete(notice);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}
    /**
    * 根据id批量删除
    */
    @Jurisdiction(value = "deletes")
    @RequestMapping(value = "/deletes",produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<String> deletes(@RequestBody List<Notice> notices){
        Letter<String> letter = new Letter<String>();
        if(DataTool.isBlank(notices)) {
            letter.setResult("FAIL");
            letter.setDetail("信息不能为空！");
            return letter;
        }
        try {
			noticeService.deletes(notices);
            letter.setDetail("数据删除成功");
            letter.setResult("SUCCESS");
        }catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据删除失败！");
        }
        return letter;
    }

	/**
	 * 根绝id更新
	 * @param notice
	 * @return
	 */
	@Jurisdiction(value = "updateById")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody Notice notice){
		Letter<String> letter = new Letter<String>();
		if(notice.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			noticeService.updateById(notice);
			letter.setDetail("数据修改成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据修改失败！");
		}
		return letter;
	}
}
